package com.alameda.config.mvc;

import com.alameda.config.interceptors.ApiSecureInterceptor;
import com.alameda.config.interceptors.LoginInterceptor;
import com.alameda.config.interceptors.MdcLogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * mvc配置类用于配置拦截器
 *
 * @author Lv
 * @date 2024/2/19
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private MdcLogInterceptor mdcLogInterceptor;

    @Autowired
    private ApiSecureInterceptor apiSecureInterceptor;

    /**
     * 系统白名单
     */
    @Value("${system.white.list}")
    private List<String> systemWhiteList;

    /**
     * 组件白名单
     */
    @Value("${commonpent.white.list}")
    private List<String> commonpentWhiteList;

    /**
     * 添加拦截器并指定不拦截的路径
     *
     * @param registry 拦截器注册表
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 安全拦截除组件外的所有接口
        registry.addInterceptor(apiSecureInterceptor).excludePathPatterns(
                commonpentWhiteList

                // 允许访问并使用knife4j
                /// "/doc.html", "/webjars/**", "/img.icons/**", "/swagger-resources/**", "/v2/api-docs",
                // 允许访问并使用代码生成器
                /// "/api/v1/load_table", "/api/v1/generator_download", "/api/v1/get_application_name", "/dist/index.html", "/dist/static/css/**",
                /// "/favicon.ico", "/dist/static/js/**", "/dist/static/fonts/**"
        );

        // 日志拦截所有请求
        registry.addInterceptor(mdcLogInterceptor).addPathPatterns("/**");

        // 登录拦截 登录接口和注册接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns(
                systemWhiteList

                // 允许访问并使用登录注册相关接口
                /// "/airborne/login", "/airborne/register", "/airborne/get_img_code",
                // 允许访问并使用knife4j
                /// "/doc.html", "/webjars/**", "/img.icons/**", "/swagger-resources/**", "/v2/api-docs",
                // 允许访问并使用代码生成器
                /// "/api/v1/load_table", "/api/v1/generator_download", "/api/v1/get_application_name", "/dist/index.html", "/dist/static/css/**",
                /// "/favicon.ico", "/dist/static/js/**", "/dist/static/fonts/**"
        );
    }
}
